package com.servlet;

import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
import com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserSevlet extends HttpServlet {
    private UserDao userDao=new UserDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri=req.getRequestURI();
        int i=uri.lastIndexOf("/");
        String s=uri.substring(i+1);
        switch (s){
            case "getAllUser":
                getAllUser(req,resp);
                break;
            case "addUser":
                addUser(req,resp);
                break;
            case "delUser":
                delUser(req,resp);
                break;
            case "updateUser":
                updateUser(req,resp);
                break;
            case "getOneUserById":
                getOneUserById(req,resp);
                break;
        }
    }
    protected void getAllUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User>users=userDao.gerAllUser();
        req.setAttribute("users",users);
        req.getRequestDispatcher("/showUser.jsp").forward(req,resp);
    }
    protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        User user=new User(null,username,password);
        int i=userDao.insertUser(user);
        getAllUser(req,resp);
    }
    protected void delUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String id=req.getParameter("id");
        int i=userDao.delUserById(id);
        getAllUser(req,resp);
    }
    protected void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     req.setCharacterEncoding("utf-8");
     resp.setContentType("text/html;charset=utf-8");
     String id=req.getParameter("id");
     String username=req.getParameter("username");
     String password=req.getParameter("password");
     User user=new User(Integer.parseInt(id),username,password);
     int i=userDao.updateUser(user);
     getAllUser(req,resp);
    }
    protected void getOneUserById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        User user=userDao.getOneUserById(id);
        req.setAttribute("user",user);
        req.getRequestDispatcher("/updateUser.jsp").forward(req,resp);
    }
}
